home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / GRAPHDEM.ICN < prev    next >
Text File  |  1992-12-29  |  4KB  |  161 lines

  1. ############################################################################
  2. #
  3. #    File:     graphdem.icn
  4. #
  5. #    Subject:  Program to demonstrate simple bar graphics
  6. #
  7. #    Author:   Matthias Heesch
  8. #
  9. #    Date:     September 28, 1992
  10. #
  11. ###########################################################################
  12. #
  13. #  graph.icn: simple bar graphics package with two demo applications:
  14. #    1. display the 4 most frequently used characters in a string.
  15. #    2. display the fibonacci numbers
  16. #
  17. ############################################################################
  18. #
  19. #  Requires: ANSI terminal support
  20. #
  21. ############################################################################
  22.  
  23. procedure main()
  24.     local option
  25.  
  26.     write("graph: simple bar graphics package for icon")
  27.     write("(b)yte frequency count or (f)ibonacci's numbers?")
  28.     option := read()
  29.     case option of {
  30. "b"     : countdemo()
  31. "f"     : fibodemo()
  32. default : write("erroneous option")
  33.     }
  34. end
  35. #
  36. procedure countdemo()
  37.     local numlist, line, a, ms, b
  38.  
  39.     numlist := list(0)
  40.     write("type strings or quit using end-of-file")
  41.     while line := read() do {
  42.     a := frequ_count(line,4)
  43.     ms := a
  44.     a ? {
  45.         while b := tab(upto(";")) do {
  46.         b ? {
  47.             tab(upto(","))
  48.             move(1)
  49.             b := tab(0)
  50.         }
  51.         move(1)
  52.         put(numlist,b)
  53.         }
  54.     }
  55.     graph(numlist,("the most frequently used characters: " || ms))
  56.     }
  57. end
  58. #
  59. procedure frequ_count(lin,item_number)
  60.     local result, n, byte_frequency_1, byte_frequency_2, byte, entry
  61.  
  62.     result := ""
  63.     n := 1
  64.     byte_frequency_1 := table(0)
  65.     every byte := !lin do {
  66.         byte_frequency_1[byte] +:= 1
  67.     }
  68.     byte_frequency_2 := sort(byte_frequency_1,2)
  69.     while n <= item_number do {
  70.     entry := pull(byte_frequency_2)
  71.     result := result || pop(entry) || "," || pull(entry) || ";"
  72.     n +:= 1
  73.     }
  74. return result
  75. end
  76. #
  77. # fibodemo(): calls user defined function fibo(n,m): fibodemo() will
  78. # use an ansi escape code to clear the screen after every call to
  79. # graph. therefore when using ms/dr dos the config.sys file should
  80. # contain: device=ansi.sys. using other operating systems, the line
  81. # containing the esc-code should be deleted.
  82. procedure fibodemo()
  83.     local a, l, b, fb
  84.  
  85.     while every a := fibo(0,1) & a < 10000 do {
  86.     l := list(4,0)
  87. # delete the following line if you don't use ms/dr dos
  88.     write(char(27),"[2J")
  89.     l[1] := a
  90.     graph(l,("fibo: " || a || ".   <enter> to continue"))
  91.     b := read()
  92.     }
  93. end
  94. #
  95. procedure fibo(m,n)
  96.     local fb
  97.  
  98.     while n < 30000 do {
  99.     fb := m + n
  100.     m := n
  101.     n := fb
  102.     suspend fb
  103.     }
  104. end
  105. #
  106. # graph(numbers,comment): bar graphics function which accepts a list
  107. # of 4 integers 10000 and a commentary message. it will display 4
  108. # bar graphic diagrams which each contains a diagram of one of the
  109. # argument values. in the order of the decimal system, the left bar
  110. # shows the 1000s, the following the 100s etc. Therefore the values
  111. # have to be <10000. When the diagram has been displayed argument
  112. # comment will be written to the screen.
  113. procedure graph(numbers,comment)
  114.     local item, itm, value, bar, graph_line, l, m, n, nn
  115.  
  116. # item2 is a list which contains lists of each 4 strings. these strings
  117. # correspond to the numerical values in the lists contained in list
  118. # numbers. each of these strings contains repl(" ",(10-numerical_value))
  119. # || repl("▄",numerical_value).
  120. #
  121. # create item2 with its string contents
  122.     item := list(0)
  123.      while itm := pop(numbers) do {
  124. # write every place of itm if there are less then 4 places.
  125.         if *itm < 4 then itm := repl("0",(4 - *itm)) || itm
  126. # convert every place of itm to a "▄ "-string and assign it
  127. # to list item
  128.         while every value := !itm do {
  129.           bar := repl(" ",(10 - value)) || repl("▄",value)
  130.           put(item,bar)
  131.         }
  132.     }
  133. # display bar graphic
  134.     graph_line := ""
  135.     l := 1
  136.     m := 1
  137.     n := 1
  138.     nn := 10
  139.     while n <= 10 do {
  140.       while m <= 16 do {
  141.       while l <= 4 do {
  142.         graph_line := graph_line || " " || !item[m]
  143.         item[m][1] := ""
  144.         l +:= 1
  145.         m +:= 1
  146.       }
  147.       graph_line := graph_line || "  ║  "
  148.       l := 1
  149.     }
  150.       write(graph_line,"    ",nn)
  151.       graph_line := ""
  152.       l := 1
  153.       m := 1
  154.       n +:= 1
  155.       nn -:= 1
  156.     }
  157.     write(" a b c d")
  158.     write("a: 1000, b: 100, c: 10, d: 1")
  159.     write(comment)
  160. end
  161.